/**
 * Created by 诗觞 on 2016/8/14.
 */
$(function(){
	// 添加文字功能
	var $readtext=$(".write-text");
	var $textnote=$(".text-note");
	var $addnote=$(".add-note");
	var mouseone=0;
	var mousetwo=0;
	var flag=false;
	$(".addtoleft").show();
	$readtext.mousedown(function(e){
		flag=true;
		$addnote.addClass("hidden");
	});
	$readtext.mouseup(function(e){
		var text='';
		if (flag) {
			mouseone=e.pageX;
			mousetwo=e.pageY;
			var timeout=setTimeout(function(){
				text=window.getSelection().toString();
				if (text.length>1) {
					$addnote.removeClass("hidden").css("top",mousetwo+12+'px').css("left",mouseone-100+'px');
					$textnote.text(text);
				}
			},10)
		}
		flag=false;
	});
	var $formcontrol=$("[class=form-control]");
	$("[class=remove]").click(function(){
		var index=$("[class=remove]").index(this);
		$formcontrol.eq(index).val('');
	});

	$(".add-left").click(function(){
		var text=$(".text-note").text();
		for(var i=0;i<$formcontrol.length;i++){
			if ($formcontrol.eq(i).val()=='') {
				$formcontrol.eq(i).val(text);
				$addnote.addClass("hidden");
				break;
			}
		}
	});
	$(".add-note-text").click(function(){
		$addnote.addClass("hidden");
	});
	// 关闭结构图
	var $close=$("#close");
	var $chart=$(".chart");
	$close.click(function(){
		// $chart.hide();
		$("#icon").find('i').removeClass("fa-arrow-up").addClass("fa-arrow-down");
		$chart.stop(true).animate({left:"400px",top:"0"},200,function(){
			$chart.hide();
		});
	});
	// 打开结构图
	var $open=$(".mychart");
	$open.click(function(){
		$("#icon").find('i').removeClass("fa-arrow-down").addClass("fa-arrow-up");
		$chart.show().stop(true).animate({left:"400px",top:"200px"},200);
	});

	var isMove=false;
	var $move=$(".closeit");
	var left=0;
	var top=0;
	$move.mousedown(function(e){
		isMove=true;
		left=e.pageX-$move.offset().left;
		top=e.pageY-$move.offset().top;
	});
	$(window).mousemove(function(e){
		if (isMove) {
			$chart.css({"left":e.pageX-left+'px',"top":e.pageY-top+'px'});
		}
	});
	$(window).mouseup(function(e){
		isMove=false;
	});


	// 检测字数多少
	var $write=$("#composition");
	var write='';
	var $words=$(".word-count span");
	$words.text('0');
	$write.keydown(function(){
		var $this=$(this);
		write=$this.text();
		var parts=write.split(/[^a-zA-Z\d]+/);
		$words.text(parts.length+'');
	})
	var $writex=$("#compositionx");
	$words.text('0');
	$writex.keydown(function(){
		var $this=$(this);
		write=$this.text();
		var parts=write.split(/[^a-zA-Z\d]+/);
		$words.text(parts.length+'');
	})
});
// 将文字转化为段落并删除前后空格
function writeToType(val){
	var lines=[];
	var returnType='';
	lines=val.split(/\n\s*/);
	for (var i = 0; i < lines.length; i++) {
		lines[i]=lines[i].trim();
		returnType+='<p>'+lines[i]+'</p>';
	}
	return returnType;
}